package com.atguigu.controller;

import com.atguigu.pojo.Employee;
import com.atguigu.service.EmployeeService;
import com.atguigu.utils.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author Chunsheng Zhang 尚硅谷
 * @create 2022/5/4 15:38
 */
@Controller
public class EmployeeController {

    @Autowired
    @Qualifier("employeeService")
    private EmployeeService employeeService;

    /**
     * 获取所有员工信息
     * @return
     */
    @GetMapping("/emps/{pageNum}")
    public String getAllEmps(@PathVariable("pageNum") Integer pageNum,
                             HttpServletRequest request,
                             Map<String,Object> map){
        if(pageNum == null || pageNum <= 0){
            pageNum = 1;
        }
        //查询之前开启分页
        PageHelper.startPage(pageNum,3);
        //获取数据
        List<Employee> empList = employeeService.getAllEmps();
        //查询之后，封装PageInfo
        PageInfo<Employee> pageInfo = new PageInfo<Employee>(empList,5);
        //使用PageUtils获取分页信息
        String pageResult = PageUtils.getPageInfo(pageInfo, request);
        //将数据共享到域中
        map.put("empList",empList);
        //将分页结果，共享域中
        map.put("pageResult",pageResult);
        return "employee/list";
    }
    @PostMapping("/test")
    @ResponseBody
    public String test(){
        employeeService.method();
        System.out.println("employeeService1 = ");
        return "123";
    }



}
